package com.suse.service.community.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.suse.service.community.entity.User;
import com.suse.service.community.service.LikeService;
import com.suse.service.community.util.RedisUtil;
import com.suse.service.community.util.UserHolder;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 点赞业务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-09-02 13:38
 */
@Service
public class LikeServiceImpl implements LikeService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void like(int entityType, int entityId, int entityUserId) {
        redisTemplate.execute(new SessionCallback<Object>() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String entityLikeKey = RedisUtil.getEntityLikeKey(entityId);
                String userLikeKey = RedisUtil.getUserLikeKey(entityUserId);
                User user = UserHolder.getUser();
                Integer userId = user.getId();
                Boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);
                // 多个操作
                operations.multi();
                if (BooleanUtil.isTrue(isMember)) {
                    operations.opsForSet().remove(entityLikeKey,userId);
                    operations.opsForValue().decrement(userLikeKey);
                }else {
                    operations.opsForSet().add(entityLikeKey,userId);
                    operations.opsForValue().increment(userLikeKey);
                }
                return operations.exec();
            }
        });
    }

    @Override
    public long getEntityLikeCount(int entityType, int entityId) {
        String entityLikeKey = RedisUtil.getEntityLikeKey(entityId);
        Long size = redisTemplate.opsForSet().size(entityLikeKey);
        if (ObjectUtil.isNull(size)) {
            return 0;
        }else {
            return size;
        }
    }

    @Override
    public int getEntityLikeStatus(int entityType, int entityId) {
        User user = UserHolder.getUser();
        // 用户没有登录 默认显示没有点赞
        if (ObjectUtil.isNull(user)) {
            return 0;
        }
        Integer userId = user.getId();
        String entityLikeKey = RedisUtil.getEntityLikeKey(entityId);
        Boolean isMember = redisTemplate.opsForSet().isMember(entityLikeKey, userId);
        if (BooleanUtil.isTrue(isMember)) {
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int getUserLikeCount(int userId) {
        String userLikeKey = RedisUtil.getUserLikeKey(userId);
        Object value = redisTemplate.opsForValue().get(userLikeKey);
        if (ObjectUtil.isNull(value)) {
            return 0;
        }else {
            return Integer.parseInt(String.valueOf(value));
        }
    }

}
